package com.hiabr.iep.admin.controller;

import com.hiabr.iep.admin.api.UserService;
import com.hiabr.iep.admin.vo.req.LoginReqVo;
import com.hiabr.iep.admin.vo.req.RefreshTokenReqVo;
import com.hiabr.iep.admin.vo.resp.LoginRespVo;
import com.hiabr.iep.admin.vo.resp.UserInfoRespVo;
import com.hiabr.iep.common.domain.vo.resp.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 登录 前端控制器
 * </p>
 *
 * @author jie huang
 * @since 2023-06-28
 */
@RestController
public class LoginController {

    @Lazy
    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param req
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<LoginRespVo> login(@Valid @RequestBody LoginReqVo req) {
        LoginRespVo vo = userService.login(req);
        return BaseResponse.ok(vo);
    }

    /**
     * 登出
     *
     * @return
     */
    @DeleteMapping("/logout")
    public BaseResponse<?> logout() {
        userService.logout();
        return BaseResponse.ok();
    }

    /**
     * 刷新token
     *
     * @param req
     * @return
     */
    @PutMapping("/refreshToken")
    public BaseResponse refreshToken(@Valid @RequestBody RefreshTokenReqVo req) {
        LoginRespVo login = userService.refreshToken(req);
        return BaseResponse.ok(login);
    }

    /**
     * 查询登录用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public BaseResponse<UserInfoRespVo> info() {
        return BaseResponse.ok(null);
    }

}
